//
//  VideoPlayViewController.m
//  ZXVideoPlayer
//
//  Created by Shawn on 16/4/29.
//  Copyright © 2016年 Shawn. All rights reserved.
//

#import "VideoPlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "playingViewController.h"
#import "DynamicTrainVC.h"
#import "ZXVideoPlayerController.h"
#import "ZXVideo.h"
#import "recoveriesModel.h"
#import "AppHeader.h"
#import "DynamicTrainVC.h"
#import "RightModel.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface VideoPlayViewController ()<ZFPlayerDelegate>
@property(nonatomic,strong) UIView *playerFatherView;

@property (nonatomic, strong) ZFPlayerView *videoController;

@property (nonatomic, assign) BOOL isPlaying;

@property (nonatomic, strong) ZFPlayerModel *playerModel;

@property (nonatomic, strong) ZFPlayerControlView *controlView;

@property(nonatomic,strong) UIButton *likeBtn;

@property(nonatomic,strong) UIButton *commentBtn;

@property(nonatomic,strong) UILabel *desLabel;

@property(nonatomic,strong) UILabel *timeLable;

@property(nonatomic,strong) UIButton *stopBtn;

@property(nonatomic,strong) UIButton *nextBtn;

@property(nonatomic,strong) UIButton *preBtn;

@property (nonatomic, assign) NSInteger index;

@property(nonatomic,strong) NSArray *arrData;

//@property(nonatomic,strong) playingViewController *playVC;

@end

@implementation VideoPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController setNavigationBarHidden:YES animated:YES];

    self.view.backgroundColor = [UIColor ColorWithHex:@"#474747"];
    self.index = 0;
    [self playVideo];
    [self initUI];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.videoController pause];
}

- (void)playVideo{
    if (!self.videoController) {
        self.playerFatherView = [[UIView alloc] init];
        [self.view addSubview:self.playerFatherView];
        [self.playerFatherView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.leading.trailing.mas_equalTo(0);
            // 这里宽高比16：9,可自定义宽高比
            make.height.mas_equalTo(self.playerFatherView.mas_width).multipliedBy(9.0f/16.0f);
        }];
        self.videoController = [[ZFPlayerView alloc]init];
        [self.playerFatherView addSubview:self.videoController];
        [self.videoController mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(20);
            make.left.right.equalTo(self.view);
            make.height.equalTo(self.videoController.mas_width).multipliedBy(9.0f/16.0f);
        }];
        
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.videoController play];
}

-(void)initUI{
    
    self.playerModel = [[ZFPlayerModel alloc]init];
    _playerModel.videoURL = [NSURL URLWithString:self.videoUrl];
    _playerModel.title = self.titles;
    _playerModel.placeholderImage = [UIImage imageNamed:@""];
    _playerModel.fatherView       = self.playerFatherView;
    [self.videoController playerControlView:_controlView playerModel:_playerModel];
    self.videoController.delegate = self;
    [self.videoController autoPlayTheVideo];
    
    
    self.desLabel = [[UILabel alloc]init];
    [self.view addSubview:self.desLabel];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.playerFatherView.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(15);
    }];
    self.desLabel.textColor = [UIColor whiteColor];
    self.desLabel.textAlignment = NSTextAlignmentLeft;
    self.desLabel.font = [UIFont systemFontOfSize:17];
    
//    self.commentBtn = [[UIButton alloc]init];
//    [self.view addSubview:self.commentBtn];
//    [self.commentBtn setImage:[UIImage imageNamed:@"review"] forState:UIControlStateNormal];
//    [self.commentBtn addTarget:self action:@selector(commentBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.view).offset(-20);
//        make.centerY.equalTo(self.desLabel.mas_centerY).offset(3);
//        make.width.height.equalTo(@30);
//    }];
    
    UILabel *lineLbl = [[UILabel alloc]init];
    lineLbl.backgroundColor = [UIColor ColorWithHex:@"#616360"];
    [self.view addSubview:lineLbl];
    [lineLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.desLabel.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.equalTo(@2);
    }];
    
    self.timeLable = [[UILabel alloc]init];
    [self.view addSubview:self.timeLable];
    [self.timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineLbl.mas_bottom).offset(40);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    self.timeLable.font = [UIFont systemFontOfSize:25];
    self.timeLable.textColor = [UIColor whiteColor];
//    self.timeLable.text = @"00:04/01:30";
    
//    self.likeBtn = [[UIButton alloc]init];
//    [self.view addSubview:self.likeBtn];
//    [self.likeBtn setImage:[UIImage imageNamed:@"admire"] forState:UIControlStateNormal];
//    [self.likeBtn addTarget:self action:@selector(likeBtnDIidClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.commentBtn.mas_left).offset(-20);
//        make.centerY.equalTo(self.desLabel.mas_centerY);
//        make.width.height.equalTo(@30);
//    }];
 
    self.stopBtn = [[UIButton alloc]init];
    [self.view addSubview:self.stopBtn];
    [self.stopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineLbl.mas_bottom).offset(100);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.height.equalTo(@90);
    }];
    [self.stopBtn setBackgroundImage:[UIImage imageNamed:@"done"] forState:UIControlStateNormal];
    [self.stopBtn setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    [self.stopBtn addTarget:self action:@selector(stopBtnDidClick) forControlEvents:UIControlEventAllTouchEvents];
    
    self.nextBtn = [[UIButton alloc]init];
    [self.view addSubview:self.nextBtn];
    [self.nextBtn setImage:[UIImage imageNamed:@"right"] forState:UIControlStateNormal];
    [self.nextBtn addTarget:self action:@selector(nextBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-50);
        make.centerY.equalTo(self.stopBtn.mas_centerY);
        make.width.equalTo(@30);
        make.height.equalTo(@90);
    }];
    
    
    self.preBtn = [[UIButton alloc]init];
    [self.view addSubview:self.preBtn];
    [self.preBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [self.preBtn addTarget:self action:@selector(preBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.preBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(50);
        make.centerY.equalTo(self.stopBtn.mas_centerY);
        make.width.equalTo(@30);
        make.height.equalTo(@90);
    }];
    
    recoveriesModel *model = self.dataArr[self.index];
    self.desLabel.text = [NSString stringWithFormat:@"%ld/%lu%@",(long)self.index + 1 ,(unsigned long)self.dataArr.count,model.title];
}

// 返回值要必须为NO
- (BOOL)shouldAutorotate
{
    return NO;
}

#pragma mark - ZFPlayerDelegate

- (void)zf_playerBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (ZFPlayerControlView *)controlView
{
    if (!_controlView) {
        _controlView = [[ZFPlayerControlView alloc] init];
    }
    return _controlView;
}

-(void)stopBtnDidClick{
    [self.videoController pause];
    playingViewController *vc = [playingViewController new];
    [self presentViewController:vc  animated:YES completion:nil];
    
}
-(void)nextBtnDidClick{
    self.index ++;
    [self LoadData];

}
-(void)preBtnDidClick{
    self.index --;
    [self LoadData];
}
-(void)LoadData{
    
    if (self.index < 0){
        self.preBtn.enabled = NO;
        return;
    }else{
        self.preBtn.enabled = YES;
    }
    
    if (self.index > self.dataArr.count - 1){
        self.nextBtn.enabled = NO;
        return;
    }else{
        self.nextBtn.enabled = YES;
    }
    
    recoveriesModel *model = self.dataArr[self.index];
    self.desLabel.text = [NSString stringWithFormat:@"%ld/%lu%@",(long)self.index +1 ,(unsigned long)self.dataArr.count,model.title];
    self.playerModel = [[ZFPlayerModel alloc]init];
    _playerModel.videoURL = [NSURL URLWithString:model.videoAddress];
    _playerModel.title = model.title;
    _playerModel.placeholderImage = [UIImage imageNamed:@""];
    _playerModel.fatherView       = self.playerFatherView;
    self.videoController.delegate = self;
    [self.videoController resetToPlayNewVideo:self.playerModel];

}

@end
